<?php

namespace Home\Model;
use Think\Model;

/**
 * Description of UserLotteryInfo
 *
 * @author Dark
 */
class UserLotteryInfoModel extends Model {
    /*
     * 增加一条用户拼购信息
     */
    public function addInfo($data) {
        if(!empty($data)) {
            $this->create();
            return $this->add($data);
        } else {
            return FALSE;
        }
    }

    /*
     * 用户删除信息
     */
    public function userDeleteByID($id) {
        $data['isUserDelete'] = 1;
        if(!empty($id)) {
            return $this->where('id='.$id)->save($data);
        } else {
            return FALSE;
        }
    }
    /*
     * 从数据库删除
     */
    public function deleteByID($id) {
        //return $this->where("ID=".$id)->delete();
        $sql="UPDATE EachGo.`t_user_lottery_info` SET isReceive=TRUE WHERE ID=".$id;
        return $this->execute($sql);
    }
    /*
     * 查看某用户的拼购信息
     */
    public function queryByUserID($userID) {
        if(!empty($userID)) {
            return $this->where('user_ID='.$userID)->select();
        } else {
            return FALSE;
        }
    }
    /*
     * 查看所有的拼购信息
     */
    public function queryAll() {
        return $this->select();
    }
    /*
     * 根据时间查看拼购信息
     */
    public function queryByTime($time,$userID) {
        switch ($time) {
            case 1: 
                $sql = 'select * from EachGo.t_user_lottery_info where user_ID='.$userID.' and joinDate>DATE_SUB(CURDATE(), INTERVAL 1 MONTH ) ORDER BY joinDate';
                $results = $this->query($sql);
                break;
            case 3:
                $sql = 'select * from EachGo.t_user_lottery_info where user_ID='.$userID.' and joinDate>DATE_SUB(CURDATE(), INTERVAL 3 MONTH) ORDER BY joinDate';
                $results = $this->query($sql);
                break;
            case 6:
                $sql = 'select * from EachGo.t_user_lottery_info where user_ID='.$userID.' and joinDate>DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ORDER BY joinDate';
                $results = $this->query($sql);
                break;
            case 12:
                $sql = 'select * from EachGo.t_user_lottery_info where user_ID='.$userID.' and joinDate>DATE_SUB(CURDATE(), INTERVAL 12 MONTH) ORDER BY joinDate';
                $results = $this->query($sql);
                break;
            default :
                $results = $this->order('joinDate desc')->select();
                break;
        }
        return $results;
    }
    /*
     * 查询开奖或者未开奖   0未开奖，1已开奖
     */
    public function queryByStates($state) {
        if($state == 0) {
            return $this->where('isLotottery='.$state)->select();
        } else if($state == 1){
            return $this->where('isLotottery='.$state)->select();
        } else {
            return $this->select();
        }
    }
    /*
     * 更新揭晓状态
     */
    public function updateState($goodsID, $turn) {
        return $this->where('goods_ID='.$goodsID.' and lotteryTurn='.$turn)->save($data['isLottery'] = 1);
    }
    public function queryAllBingo($UserId){
            $isBingo="SELECT a.ID AS userLotterId,b.goodsName,b.`unitPrice`,a.lotteryNumber,
                    a.joinDate,a.lotteryTurn,a.joinPrice,a.goods_ID,c.luckyNumber,
                    a.isReceive
                    FROM EachGo.`t_user_lottery_info` a JOIN EachGo.`t_goods` b ON a.goods_ID=b.`ID`
                    JOIN EachGo.`t_lottery_goods_info` c ON b.`ID`=c.goods_ID
                    WHERE a.isUserDelete=FALSE AND a.`user_ID`=".$UserId." ORDER BY a.joinDate DESC";
        $reuslt=$this->query($isBingo);
        //var_dump($reuslt);
        for($i=0;$i<count($reuslt);$i++){
            $info=$reuslt[$i];
            //var_dump($info);
            $numberSql="SELECT COUNT(*) AS number FROM EachGo.`t_user_lottery_info`
              WHERE goods_ID=".$info['goods_id'];//获取当前拼购人数
            $number=$this->query($numberSql);
            $numberArray=array(
                'number'=>$number[0]['number'],
            );
            $reuslt[$i]=array_merge($info,$numberArray);//合并数组
            $total=ceil ((int)$info['unitprice']/(int)$info['joinprice']);//计算改拼购商品需要的总人数
            $reuslt[$i]=array_merge($reuslt[$i],array('total'=>$total));
            if($info['luckynumber']==$info['lotterynumber']){
                $reuslt[$i]=array_merge($reuslt[$i],array('iswin'=>1));
            }else{
                $reuslt[$i]=array_merge($reuslt[$i],array('iswin'=>0));
            }
        }
        return $reuslt;
    }
    public function shareInfo($userLotteryId){
        $sql="SELECT a.ID AS userLotterId,b.goodsName,a.lotteryTurn,lotteryNumber,b.imageURLs
        FROM EachGo.`t_user_lottery_info` a JOIN EachGo.`t_goods` b
        ON a.goods_ID = b.ID
        WHERE a.ID=".$userLotteryId;
        return $this->query($sql);
    }
}
